Here’s some of the code from Chapter 8: Making maps with R, for you to follow along with as we go through it in class.

library(sf)
## Warning: package 'sf' was built under R version 4.1.1
## Linking to GEOS 3.9.1, GDAL 3.2.3, PROJ 7.2.1
library(raster)
## Loading required package: sp
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(spData)
library(spDataLarge)
## Warning: package 'spDataLarge' was built under R version 4.1.1
library(tmap)    # for static and interactive maps
## Warning: package 'tmap' was built under R version 4.1.1
library(leaflet) # for interactive maps
library(ggplot2) # tidyverse data visualization package

8.2.1 tmap basics

nz
## Simple feature collection with 16 features and 6 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 1090144 ymin: 4748537 xmax: 2089533 ymax: 6191874
## Projected CRS: NZGD2000 / New Zealand Transverse Mercator 2000
## First 10 features:
##                 Name Island Land_area Population Median_income Sex_ratio
## 1          Northland  North 12500.561     175500         23400 0.9424532
## 2           Auckland  North  4941.573    1657200         29600 0.9442858
## 3            Waikato  North 23900.036     460100         27900 0.9520500
## 4      Bay of Plenty  North 12071.145     299900         26200 0.9280391
## 5           Gisborne  North  8385.827      48500         24400 0.9349734
## 6        Hawke's Bay  North 14137.524     164000         26100 0.9238375
## 7           Taranaki  North  7254.480     118000         29100 0.9569363
## 8  Manawatu-Wanganui  North 22220.608     234500         25000 0.9387734
## 9         Wellington  North  8048.553     513900         32700 0.9335524
## 10        West Coast  South 23245.456      32400         26900 1.0139072
##                              geom
## 1  MULTIPOLYGON (((1745493 600...
## 2  MULTIPOLYGON (((1803822 590...
## 3  MULTIPOLYGON (((1860345 585...
## 4  MULTIPOLYGON (((2049387 583...
## 5  MULTIPOLYGON (((2024489 567...
## 6  MULTIPOLYGON (((2024489 567...
## 7  MULTIPOLYGON (((1740438 571...
## 8  MULTIPOLYGON (((1866732 566...
## 9  MULTIPOLYGON (((1881590 548...
## 10 MULTIPOLYGON (((1557042 531...
# Add fill layer to nz shape
tm_shape(nz) +
  tm_fill() 

# Add border layer to nz shape
tm_shape(nz) +
  tm_borders() 

# Add fill and border layers to nz shape
tm_shape(nz) +
  tm_fill() +
  tm_borders() 

qtm(nz) +
  qtm(nz_height)

8.2.2 Map objects

map_nz = tm_shape(nz) +
  tm_polygons()
class(map_nz)
## [1] "tmap"
map_nz

map_nz1 = map_nz +
  tm_shape(nz_elev) +
  tm_raster(alpha = 0.7)

map_nz1
## stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)

nz_water = st_union(nz) %>%
  st_buffer(22200) %>% 
  st_cast(to = "LINESTRING")

plot(nz_water)

map_nz2 = map_nz1 +
  tm_shape(nz_water) +
  tm_lines()

map_nz2
## stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)

map_nz3 = map_nz2 +
  tm_shape(nz_height) +
  tm_dots()

map_nz3
## stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)

tmap_arrange(map_nz1, map_nz2, map_nz3)
## stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)
## stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)
## stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)
## stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)
## Some legend labels were too wide. These labels have been resized to 0.63, 0.55, 0.55, 0.55, 0.55, 0.55. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.
## stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)
## Some legend labels were too wide. These labels have been resized to 0.63, 0.55, 0.55, 0.55, 0.55, 0.55. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.
## stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)
## Some legend labels were too wide. These labels have been resized to 0.63, 0.55, 0.55, 0.55, 0.55, 0.55. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.

8.2.3 Aesthetics

ma1 = tm_shape(nz) + tm_fill(col = "red")
ma2 = tm_shape(nz) + tm_fill(col = "red", alpha = 0.3)
ma3 = tm_shape(nz) + tm_borders(col = "blue")
ma4 = tm_shape(nz) + tm_borders(lwd = 3)
ma5 = tm_shape(nz) + tm_borders(lty = 2)
ma6 = tm_shape(nz) + tm_fill(col = "red", alpha = 0.3) +
  tm_borders(col = "blue", lwd = 3, lty = 2)

tmap_arrange(ma1, ma2, ma3, ma4, ma5, ma6)

plot(st_geometry(nz), col = nz$Land_area)  # works

# tm_shape(nz) + tm_fill(col = nz$Land_area) # fails
tm_shape(nz) + tm_fill(col = "Land_area")

legend_title = expression("Area (km"^2*")")
map_nza = tm_shape(nz) +
  tm_fill(col = "Land_area", title = legend_title) + tm_borders()

map_nza

8.2.4 Color settings

tm_shape(nz) + tm_polygons(col = "Median_income")

breaks = c(0, 3, 4, 5) * 10000
tm_shape(nz) + tm_polygons(col = "Median_income", breaks = breaks)

tm_shape(nz) + tm_polygons(col = "Median_income", n = 10)

tm_shape(nz) + tm_polygons(col = "Median_income", palette = "BuGn")

tm_shape(nz) + tm_polygons("Population", palette = "Blues")
## Some legend labels were too wide. These labels have been resized to 0.64, 0.59, 0.59. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.

tm_shape(nz) + tm_polygons("Population", palette = "YlOrBr")
## Some legend labels were too wide. These labels have been resized to 0.64, 0.59, 0.59. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.

8.2.5 Layouts

map_nz + 
  tm_compass(type = "8star", position = c("left", "top")) +
  tm_scale_bar(breaks = c(0, 100, 200), text.size = 1)

map_nz + tm_layout(title = "New Zealand")

map_nz + tm_layout(scale = 5)

map_nz + tm_layout(bg.color = "lightblue")

map_nz + tm_layout(frame = FALSE)

map_nza + tm_layout(frame.lwd = 5)

map_nza + tm_layout(inner.margins = 0.2)

map_nza + tm_layout(legend.show = FALSE)

map_nza + tm_layout(legend.position = c("right","bottom"))

map_nza + tm_style("bw")

map_nza + tm_style("classic")

map_nza + tm_style("cobalt")

map_nza + tm_style("col_blind")

8.2.6 Faceted maps

urb_1970_2030 = urban_agglomerations %>% 
  filter(year %in% c(1970, 1990, 2010, 2030))

tm_shape(world) +
  tm_polygons() +
  tm_shape(urb_1970_2030) +
  tm_symbols(col = "black", border.col = "white", size = "population_millions") +
  tm_facets(by = "year", nrow = 2, free.coords = FALSE)

8.2.7 Inset maps

nz_region = st_bbox(c(xmin = 1340000, xmax = 1450000,
                      ymin = 5130000, ymax = 5210000),
                    crs = st_crs(nz_height)) %>%
  st_as_sfc()

nz_region
## Geometry set for 1 feature 
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 1340000 ymin: 5130000 xmax: 1450000 ymax: 5210000
## Projected CRS: NZGD2000 / New Zealand Transverse Mercator 2000
## POLYGON ((1340000 5130000, 1450000 5130000, 145...
nz_height_map = tm_shape(nz_elev, bbox = nz_region) +
  tm_raster(style = "cont", palette = "YlGn", legend.show = TRUE) +
  tm_shape(nz_height) +
  tm_symbols(shape = 2, col = "red", size = 1) +
  tm_scale_bar(position = c("left", "bottom"))

nz_height_map
## stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)

nz_map = tm_shape(nz) +
  tm_polygons() +
  tm_shape(nz_height) +
  tm_symbols(shape = 2, col = "red", size = 0.1) + 
  tm_shape(nz_region) +
  tm_borders(lwd = 3)

nz_map

library(grid)

nz_height_map
## stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)
print(nz_map, vp = viewport(0.8, 0.27, width = 0.5, height = 0.5))

us_states_map = tm_shape(us_states, projection = 2163) +
  tm_polygons() + 
  tm_layout(frame = FALSE)

us_states_map

hawaii_map = tm_shape(hawaii) +
  tm_polygons() + 
  tm_layout(title = "Hawaii", frame = FALSE, bg.color = NA, 
            title.position = c("LEFT", "BOTTOM"))

alaska_map = tm_shape(alaska) +
  tm_polygons() + 
  tm_layout(title = "Alaska", frame = FALSE, bg.color = NA)

hawaii_map

alaska_map

us_states_map
print(hawaii_map, vp = grid::viewport(0.35, 0.1, width = 0.2, height = 0.1))
print(alaska_map, vp = grid::viewport(0.15, 0.15, width = 0.3, height = 0.3))